#include "LogIn.h"

vector<QString> schoolName;
vector<QString> passwords;
vector<QString> question;
vector<QString> anwser;
//schoolName和passwords的文件读写
void saveFile(vector<QString>& schoolName, vector<QString>& passwords, vector<QString>& question, vector<QString>& anwser, const QString& filename) {
	QFile file(filename);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
		qWarning("无法打开文件进行写入");
		return;
	}

	QTextStream out(&file);

	// 使用"|"作为分隔符，你也可以选择其他不会出现在账号密码中的字符
	for (int i = 0; i < schoolName.size(); ++i) {
		out << schoolName[i] << "|" << passwords[i] << "|" << question[i] << "|" << anwser[i] << "\n";
	}

	file.close();
}
void loadFile(vector<QString>& schoolName, vector<QString>& passwords, vector<QString>& question, vector<QString>& anwser, const QString& filename) {
	QFile file(filename);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		qWarning("无法打开文件进行读取");
	}

	schoolName.clear();
	passwords.clear();
	question.clear();
	anwser.clear();
	QTextStream in(&file);
	while (!in.atEnd()) {
		QString line = in.readLine();
		QStringList parts = line.split("|", Qt::SkipEmptyParts);

		if (parts.size() == 4) {
			schoolName.push_back(parts[0]);
			passwords.push_back(parts[1]);
			question.push_back(parts[2]);
			anwser.push_back(parts[3]);
		}
	}

	file.close();
}



LogIn::LogIn(QWidget *parent)
	: QWidget(parent)
{
	this->setWindowIcon(QIcon("Image\\运动会.svg"));
	QPixmap pixmap("Image\\光标.svg");
	pixmap = pixmap.scaled(30, 24, Qt::KeepAspectRatio);
	QCursor cursor(pixmap);
	this->setCursor(cursor);
	ui.setupUi(this);
	connect(ui.loginbtn,&QPushButton::clicked,this,&LogIn::login);
}

LogIn::~LogIn()
{

}

void LogIn::login() {
	//QString fileName = "C:/Users/lsh20/Desktop/c++design/c++course/schoolInfo.txt";
	QString fileName = "../schoolInfo.txt";
	loadFile(schoolName, passwords, question, anwser, fileName);
	readPassword = ui.password->text();
	readSchool = ui.schoolName->text();
	readQuestion = ui.questionBox->currentText();
	readAnwser = ui.anwser->text();
	int flag = 1;
	for (int i = 0;i < schoolName.size();i++) {
		if (readSchool == schoolName[i]) {
			flag = 0;
		}
	}
	if (flag) {
		if (readPassword.isEmpty() || readSchool.isEmpty() || readAnwser.isEmpty()) {
			message("提示", "存在未填写的项！", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
		}
		else {
			//	
			schoolName.push_back(readSchool);
			passwords.push_back(readPassword);
			question.push_back(readQuestion);
			anwser.push_back(readAnwser);
			saveFile(schoolName, passwords, question, anwser, fileName);
			message("提示", "成功注册！", QMessageBox::Icon::Information, QMessageBox::Ok, this);
			this->close();
		}
	}
	else {
		message("提示", "该高校已注册！", QMessageBox::Icon::Warning, QMessageBox::Ok, this);
	}
	
}
void LogIn::paintEvent(QPaintEvent* event) {
	QPainter painter(this);
	QString imagePath = "image\\背景4.jpg";

	// 检查路径是否存在
	if (!QFile::exists(imagePath)) {
		qDebug() << "图片文件不存在：" << imagePath;
	}
	else {
		QPixmap bkgnd(imagePath);
		if (bkgnd.isNull()) {
			qDebug() << "无法加载图片：" << imagePath;
			painter.fillRect(rect(), QColor("#2c3e50"));

			painter.setPen(Qt::white);
			painter.drawText(rect(), Qt::AlignCenter, "背景加载失败");
			return;
		}
		// 确认窗口有效尺寸
		if (width() <= 0 || height() <= 0) {
			qDebug() << "无效窗口尺寸：" << size();
			return;
		}

		// 高质量缩放
		bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
		painter.setOpacity(0.7);  //0.7
		painter.drawPixmap(0, 0, bkgnd);
	}
}